import { handle_socket_massage } from "@/handle";

let socket_host="127.0.0.1";

let socket_port=9998;


let web_socket = null; 

let data_socket_time = 10 * 1000; //毫秒数
let data_local_time = 0;
let data_timer = null; //计时器。


function formatter_socket_time(time_str) {
  return (
    time_str.slice(0, 4) +
    "-" +
    time_str.slice(4, 6) +
    "-" +
    time_str.slice(6, 8) +
    " " +
    time_str.slice(8, 10) +
    ":" +
    time_str.slice(10, 12) +
    ":" +
    time_str.slice(12, 14)
  );
}

function websocket_init() {
  data_local_time = new Date().getTime(); //当前时间毫秒数。

  try {
    web_socket=null;

    web_socket = new WebSocket("ws://"+socket_host+":"+socket_port);

  } catch (e) {
    console.log("%cwebsocket_初始化失败_>>", "color:skyblue", e);
  }

  web_socket.onopen = websocket_on_open;
  web_socket.onmessage = websocket_on_message;
  web_socket.onclose = websocket_on_close;
  web_socket.onerror = websocket_on_error;

}

function websocket_on_open() {
  console.log(socket_port+"connecting ..");
  
}

function websocket_on_message(res) {
  console.log(socket_port+" onmessages!!!=====");
  console.log("on_message_res",res)

  data_local_time = new Date().getTime(); //当前时间毫秒数。

  let date_str='';
  try{
    date_str=formatter_socket_time(JSON.parse(res.data)[0].timestamp + "");
    data_socket_time = new Date(date_str).getTime(); //2022-01-05 09:03:31

  }catch(e){
    console.log("收到的消息是反馈",JSON.parse(res.data));
    data_socket_time=data_local_time;
  }
 

  // store.commit("set_net_disconnect", false);

  handle_socket_massage(res);
}

function websocket_on_close() {
  console.log(`%c ${socket_port} closed ..`, "color:red");


  // store.commit("set_net_disconnect", true);

  data_local_time = new Date().getTime(); //当前时间毫秒数。
  data_socket_time = 10 * 1000;
}

function websocket_on_error(err) {

  console.log(`%c ${socket_port} error ..`, "color:red", err);
}

function websocket_send(cmd, arr_param) {
  var data_formatter = {
    cmd: cmd,
    data: arr_param,
  };

  console.log("%cwebsocket_send 参数", "color:skyblue", data_formatter);

  web_socket.send(JSON.stringify(data_formatter));
}
function websocket_close() {
  web_socket && web_socket.close();
  web_socket = null;

  console.log(`获取数据的 ${socket_port} socket主动关闭`);
}

function websocket_reconect() {
  web_socket = null;
  websocket_init();
  console.log(`${socket_port} socket-主动重连---`);

  setTimeout(()=>{
    // websocket_send(store.state.store_cmd,store.state.store_param_arr)
  },500)
}

/*检查socket是否掉线*/
function check_status() {    //30秒重连
  console.log("时间相差",data_local_time,data_socket_time)
  console.log("时间相差相减",data_local_time-data_socket_time)
  if (data_local_time - data_socket_time > 1000 * 10) {
    console.log(`%c socket:${socket_port} 状态已经断开`, "color: red;");
    return true;
  } else {
    return false;
  }
}

data_timer = setInterval(() => {
  console.log("data_timer计时器===");
 
  if (check_status()) {
    console.log(`%c 即将开始重连${socket_port}`,"color:green");
    websocket_reconect();
  }else{
    console.log(`%c 进入检测不重连 ${socket_port}`,"color:green");
  }
}, 1000 * 14);

console.log(
  "获取数据的 的socket 需要主动发送参数,根据断开的page记录对应的pageId"
);

export {
  web_socket,
  websocket_init,
  websocket_on_open,
  websocket_on_message,
  websocket_on_close,
  websocket_on_error,
  websocket_send,
  websocket_close,
  websocket_reconect,
};

// let param = {
//   cmd: cmd,
//   data: data_arr
// }

// 只读属性 readyState 表示连接状态，可以是以下值：

// 0 - 表示连接尚未建立。

// 1 - 表示连接已建立，可以进行通信。

// 2 - 表示连接正在进行关闭。

// 3 - 表示连接已经关闭或者连接不能打开。
